//
//  FXWelcomeViewController.swift
//  微博(Swift)
//
//  Created by V on 16/1/1.
//  Copyright © 2016年 V. All rights reserved.
//

import UIKit
import SDWebImage

class FXWelcomeViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        if let headImageURLString = FXUserAccountViewModel.sharedViewModel.userAccount?.avatar_large {
            headImageView.sd_setImageWithURL(NSURL(string: headImageURLString), placeholderImage: UIImage(named: "avatar_default_big"))
        }
        setupUI()
    }

    // MARK: - 懒加载
    // 头像
    private lazy var headImageView: UIImageView = {
        let imgView = UIImageView(image: UIImage(named: "avatar_default_big"))
        imgView.layer.cornerRadius = 45
        imgView.layer.masksToBounds = true
        return imgView
    }()
    // 提示
    private lazy var messageLabel: UILabel = {
       let lable = UILabel()
        lable.tintColor = UIColor.darkGrayColor()
        lable.font = UIFont.systemFontOfSize(16)
        lable.text = "欢迎回来"
        lable.alpha = 0
        return lable
    }()
    
    
    private func setupUI(){
        view.backgroundColor = UIColor(white: 247/255, alpha: 1)
        view.addSubview(headImageView)
        view.addSubview(messageLabel)
        // 添加约束
        headImageView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(view)
            make.top.equalTo(view).offset(200)
            make.size.equalTo(CGSizeMake(90, 90))
        }
        messageLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(headImageView)
            make.top.equalTo(headImageView.snp_bottom).offset(10)
        }

    }
    
    
    
    // MARK: - 动画
    override func viewDidAppear(animated: Bool) {
        // 更改头像约束
        headImageView.snp_updateConstraints { (make) -> Void in
            make.top.equalTo(view).offset(100)
        }
        // 执行动画
        // Damping: 阻尼 0-1 越大，弹性的幅度越小
        // initialSpringVelocity: 初始弹性速度
        UIView.animateWithDuration(1, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 0, options: [], animations: { () -> Void in
            // 执行动画，要执行约束动画的话，一定是将 layoutIfNeeded 放在动画块
            self.view.layoutIfNeeded()
            }) { (_) -> Void in
                // 阻尼动画执行完毕的回调
                UIView.animateWithDuration(1, animations: { () -> Void in
                    self.messageLabel.alpha = 1
                    }, completion: { (_) -> Void in
                        // 透明动画执行完成后，跳转控制器
                        NSNotificationCenter.defaultCenter().postNotificationName(FXSwitchRootViewControllerNotification, object: nil)
                })
        }
        
    }
        
        
    
}
